
import logging
import models

from django.utils import simplejson as json

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users
from google.appengine.api import channel

def get_active_users():
    q=db.GqlQuery('SELECT * FROM ActiveUser').fetch(100)
    return [ k.user for k in q ]


class Chat(webapp.RequestHandler):
    def get(self):
        user=users.get_current_user()
        messg=self.request.get('messg')

        jsonobj={'TYPE': 'C',
                 'SENDER':user.nickname(),
                 'MESSG':messg}

        au=get_active_users()
        for u in au:
            if u==user:
                continue
            channel.send_message(u.user_id(), messg)


def main():
    app = webapp.WSGIApplication([('/chat', Test)], debug=True)
    run_wsgi_app(app)


if __name__=='__main__':
    main()
